package com.lxy.controller;


import com.aliyun.dysmsapi20180501.Client;
import com.aliyun.dysmsapi20180501.models.SendMessageWithTemplateRequest;
import com.aliyun.dysmsapi20180501.models.SendMessageWithTemplateResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SendSmsController {

    @Autowired
    Client client;



    @PostMapping("/send")
    public  void send() throws Exception {
        // 接收短信号码。号码格式为:国际区号+号码。例如:861503871****。
        String to = "args.get(0)";
        // 发送方标识。发往中国传入签名,请在控制台申请短信签名;发往非中国地区传入senderId。
        String from = "args.get(1)";
        // 模板code
        String templateCode = "args.get(2)";
        // 短信模板变量对应的实际值,参数格式为JSON格式。如果模板中存在变量,该参数为必填项。例如:{"name":"xd","value":"hello"}
        String templateParam =" args.get(3)";
        // 上行短信扩展码
        String smsUpExtendCode =" args.get(4)";

        sendMessageWithTemplate(client, to, from, templateCode, templateParam, smsUpExtendCode);


    }

    public static void sendMessageWithTemplate(Client client, String to, String from, String templateCode, String templateParam, String smsUpExtendCode) throws Exception {
        SendMessageWithTemplateRequest req = new SendMessageWithTemplateRequest()
                .setTo(to)
                .setFrom(from)
                .setTemplateCode(templateCode)
                .setTemplateParam(templateParam)
                .setSmsUpExtendCode(smsUpExtendCode);
        SendMessageWithTemplateResponse resp = client.sendMessageWithTemplate(req);

        log.info(com.aliyun.teautil.Common.toJSONString(com.aliyun.teautil.Common.toMap(resp)));
    }



}




